package com.pz.gulimall.ware.dao;

import com.pz.gulimall.ware.entity.WareSkuEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pz.gulimall.ware.entity.resultMap.RemainStockResultMap;
import com.pz.gulimall.ware.entity.resultMap.WareStockReleaseParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 商品库存
 * 
 * @author PZ
 * @email pzs@gmail.com
 * @date 2024-02-02 11:49:35
 */
@Mapper
public interface WareSkuDao extends BaseMapper<WareSkuEntity> {

    @Select("SELECT SUM(stock-stock_locked) FROM `wms_ware_sku` WHERE sku_id = #{skuId}")
    Long getSumStockBySku(@Param("skuId") Long skuId);

    @Select("SELECT stock-stock_locked>0 AS has_stock FROM wms_ware_sku WHERE sku_id = #{skuId}")
    Long queryHasStockBySkuId(@Param("skuId") Long skuId);

    List<RemainStockResultMap> queryRemainStockBatch(@Param("skuIds") List<Long> skuIds);

    @Update("UPDATE wms_ware_sku SET stock_locked = #{num} WHERE sku_id = #{skuId} AND ware_id = #{wareId}")
    void updateStockLocked(@Param("skuId")Long skuId, @Param("wareId") Long wareId, @Param("num") Integer num);

    void releaseStock(@Param("releaseParams") List<WareStockReleaseParam> releaseParams);
}
